﻿using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using FootballCourt.Data.Abstract;
using FootballCourt.Data.Configuration;
using FootballCourt.Domain.Entity;
using Microsoft.Practices.Unity;

namespace FootballCourt.Data
{
    public class FootballCourtContext : DbContext, IFootballCourtContext
    {
        static FootballCourtContext()
        {
            Database.SetInitializer<FootballCourtContext>(null);
        }

        [InjectionConstructor]
        public FootballCourtContext() { }

        public FootballCourtContext(string connectionString)
            : base(connectionString) { }

        public IDbSet<SystemRole> SystemRole { get; set; }
        public IDbSet<UserType> UserType { get; set; }

        #region Dbcontext
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            ConfigureEntityTypes(modelBuilder);
            base.OnModelCreating(modelBuilder);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
        #endregion

        #region IFootballCourtContext
        public void Commit()
        {
            this.SaveChanges();
        }
        public DbContext CurrentContext
        {
            get { return this; }
        }
        #endregion

        #region private method
        private void ConfigureEntityTypes(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new SystemRoleConfigurarion());
            modelBuilder.Configurations.Add(new UserTypeConfiguration());
        }
        #endregion

    }
}
